package jwf.http;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * http response header
 * 
 * @author michael
 * @email liyong@renren-inc.com
 * 
 */
public class Header {
    private Map<String, String> items;
    private int httpStatusCode;
    private String message;

    public Header() {
	items = new HashMap<String, String>();
	httpStatusCode = 0;
	message = "";
    }

    public void add(String k, String v) {
	items.put(k, v);
    }

    public void setStatusCode(int hc, String msg) {
	setHttpStatusCode(hc);
	if (msg != null) {
	    setMessage(msg);
	}
    }

    public int getHttpStatusCode() {
	return httpStatusCode;
    }

    public void setHttpStatusCode(int httpStatusCode) {
	this.httpStatusCode = httpStatusCode;
    }

    public String getMessage() {
	return message;
    }

    public void setMessage(String message) {
	this.message = message;
    }

    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("HTTP/1.1").append(" ");
	sb.append(getHttpStatusCode()).append(" ");
	sb.append(getMessage()).append(" ");
	sb.append(Http.CRLF);
	for (Entry<String, String> entry : items.entrySet()) {
	    sb.append(entry.getKey()).append(": ").append(entry.getValue())
		    .append(Http.CRLF);
	}
	sb.append(Http.CRLF);
	return sb.toString();
    }
}
